package com.event.management.frontend.mapper;

import com.event.management.frontend.domain.Order;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * 订单Mapper接口
 */
@Mapper
public interface OrderMapper {
    
    /**
     * 插入订单记录
     * @param order 订单对象
     * @return 影响行数
     */
    @Insert("INSERT INTO event_orders(user_id, event_id, ticket_id, quantity, total_price, status, real_name, phone, " +
            "address, zip_code, payment_method, payment_details, note, created_at, " +
            "contact_email, birth_date, country) " +
            "VALUES(#{userId}, #{eventId}, #{ticketId}, #{quantity}, #{totalPrice}, #{status}, #{realName}, #{phone}, " +
            "#{address}, #{zipCode}, #{paymentMethod}, #{paymentDetails}, #{note}, #{createdAt}, " +
            "#{contactEmail}, #{birthDate}, #{country})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Order order);
    
    /**
     * 更新订单状态为已支付
     * @param id 订单ID
     * @param paidAt 支付时间
     * @return 影响行数
     */
    @Update("UPDATE event_orders SET status = 'paid', paid_at = #{paidAt} WHERE id = #{id} AND status = 'unpaid'")
    int updateStatusToPaid(@Param("id") Long id, @Param("paidAt") Date paidAt);
    
    /**
     * 更新订单状态为已取消
     * @param id 订单ID
     * @return 影响行数
     */
    @Update("UPDATE event_orders SET status = 'cancelled' WHERE id = #{id} AND status = 'unpaid'")
    int updateStatusToCancelled(Long id);
    
    /**
     * 根据ID查询订单
     * @param id 订单ID
     * @return 订单对象
     */
    @Select("SELECT id, user_id, event_id, ticket_id, quantity, total_price, status, real_name, phone, " +
            "address, zip_code, payment_method, payment_details, note, created_at, paid_at, " +
            "contact_email, birth_date, country " +
            "FROM event_orders WHERE id = #{id}")
    Order selectById(Long id);
    
    /**
     * 根据用户ID查询订单列表
     * @param userId 用户ID
     * @return 订单列表
     */
    @Select("SELECT * FROM event_orders WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<Order> selectByUserId(Long userId);
    
    /**
     * 分页查询用户订单
     * @param userId 用户ID
     * @param offset 偏移量
     * @param limit 限制条数
     * @return 订单列表
     */
    @Select("SELECT * FROM event_orders WHERE user_id = #{userId} ORDER BY created_at DESC LIMIT #{offset}, #{limit}")
    List<Order> selectByUserIdWithPage(@Param("userId") Long userId, @Param("offset") int offset, @Param("limit") int limit);
    
    /**
     * 获取用户订单总数
     * @param userId 用户ID
     * @return 订单总数
     */
    @Select("SELECT COUNT(*) FROM event_orders WHERE user_id = #{userId}")
    int countByUserId(Long userId);
    
    /**
     * 根据条件查询订单列表
     * @param userId 用户ID
     * @param offset 偏移量
     * @param limit 限制条数
     * @param keyword 关键词（订单号、活动名称等）
     * @param status 订单状态
     * @return 订单列表
     */
    List<Order> selectFilteredOrders(
            @Param("userId") Long userId, 
            @Param("offset") int offset, 
            @Param("limit") int limit, 
            @Param("keyword") String keyword, 
            @Param("status") String status);
    
    /**
     * 根据条件统计订单数量
     * @param userId 用户ID
     * @param keyword 关键词
     * @param status 订单状态
     * @return 订单数量
     */
    long countFilteredOrders(
            @Param("userId") Long userId, 
            @Param("keyword") String keyword, 
            @Param("status") String status);
} 